<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goods">
      <!-- 面包屑 -->
      <XtxBreadcrumb v-if="goods">
        <XtxBreadcrumbItem to="/">首页</XtxBreadcrumbItem>
        <XtxBreadcrumbItem :to="'/category/' + goods.categories[0].id">{{
          goods.categories[1].name
        }}</XtxBreadcrumbItem>
        <XtxBreadcrumbItem :to="'/category/sub/' + goods.categories[0].id">{{
          goods.categories[0].name
        }}</XtxBreadcrumbItem>
        <XtxBreadcrumbItem>{{ goods.name }}</XtxBreadcrumbItem>
      </XtxBreadcrumb>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <!-- 商品预览组件 -->
          <GoodsImage :images="goods.mainPictures" />
          <GoodsSales />
        </div>
        <div class="spec">
          <GoodsName :goods="goods" />
          <GoodsSku :goods="goods" @change="changeSku" />
          <XtxNumbox
            label="数量"
            v-model="num"
            :min="1"
            :max="goods.inventory"
          />
          <XtxButton @click="addCart" type="primary" style="margin-top:20px;"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs />
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarn />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" :type="1" />
          <GoodsHot :goodsId="goods.id" :type="2" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsRelevant from './components/goods-relevant'
import GoodsImage from './components/goods-image'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import GoodsSku from './components/goods-sku'
import GoodsTabs from './components/goods-tabs'
import GoodsHot from './components/goods-hot'
import GoodsWarn from './components/goods-warn'
import { ref, watch, nextTick, provide } from 'vue'
import { findGoods } from '@/api/product'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import Message from '@/components/library/Message'
const useGoods = () => {
  const goods = ref(null)
  const route = useRoute()
  watch(
    () => route.params.id,
    newVal => {
      if (newVal && `/product/${newVal}` === route.path) {
        findGoods(route.params.id).then(({ result }) => {
          goods.value = null
          nextTick(() => {
            goods.value = result
          })
        })
      }
    },
    { immediate: true }
  )

  return goods
}
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsRelevant,
    GoodsImage,
    GoodsSales,
    GoodsName,
    GoodsSku,
    GoodsTabs,
    GoodsHot,
    GoodsWarn
  },
  setup () {
    const goods = useGoods()

    provide('goods', goods)

    const changeSku = sku => {
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }

      currSku.value = sku
    }

    const num = ref(1)

    const currSku = ref(null)
    const store = useStore()
    const addCart = () => {
      if (currSku.value && currSku.value.skuId) {
        // 本地：id skuId name picture price nowPrice count attrsText selected stock isEffective
        const {
          skuId,
          specsText: attrsText,
          inventory: stock,
          price
        } = currSku.value
        const { id, name, mainPictures } = goods.value

        store
          .dispatch('cart/asyncAddCart', {
            skuId,
            attrsText,
            stock,
            price,
            nowPrice: price,
            count: num.value,
            id,
            name,
            picture: mainPictures[0],
            selected: true,
            isEffective: true
          })
          .then(() => {
            Message({ type: 'success', text: '加入购物车成功~' })
          })
      } else {
        Message({ type: 'warn', text: '请选择完整之后加入购物车' })
      }
    }

    return { goods, changeSku, num, addCart }
  }
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
// .goods-tabs {
//   min-height: 600px;
//   background: #fff;
// }
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
